package com.example.myapplication1;

import com.example.myapplication1.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText heightInput, weightInput;
    private Button calculate;
    private TextView resultText, adviceText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        heightInput = findViewById(R.id.heightInput);
        weightInput = findViewById(R.id.weightInput);
        calculate = findViewById(R.id.calculate);
        resultText = findViewById(R.id.resultText);
        adviceText = findViewById(R.id.adviceText);

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateBMI();
            }
        });
    }

    private void calculateBMI() {
        String heightStr = heightInput.getText().toString();
        String weightStr = weightInput.getText().toString();

        if (heightStr.isEmpty() || weightStr.isEmpty()) {
            resultText.setText("请输入你的身高和体重");
            return;
        }

        try {
            float height = Float.parseFloat(heightStr);
            float weight = Float.parseFloat(weightStr);
            float bmi = weight / (height * height);
            String bmiResult = String.format("%.2f", bmi);

            resultText.setText("BMI: " + bmiResult);

            if (bmi < 18.5) {
                adviceText.setText("体重过轻，建议多吃饭，多喝水，增加蛋白质摄入");
            } else if (bmi >= 18.5 && bmi < 24.9) {
                adviceText.setText("体重正常，继续保持正常饮食");
            } else if (bmi >= 25 && bmi < 29.9) {
                adviceText.setText("体重过重，建议采取运动来减肥");
            } else {
                adviceText.setText("肥胖，建议严格控制饮食并增加一些运动");
            }
        } catch (NumberFormatException e) {
            resultText.setText("请输入有效的身高和体重数值");
            e.printStackTrace();
        }
    }
}
